using System;
using System.Collections.Generic;
using System.Linq;

namespace SubversionReports.Entities
{
    public class RepositoryCredentials : List<RepositoryCredential>
    {
        /// <summary>
        /// Gets the repository credential with the specified id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns>The repository credential with the specified id or null if not found.</returns>
        public RepositoryCredential GetById(Guid id)
        {
            return Find(delegate(RepositoryCredential repositoryCredential) { return repositoryCredential.Id.Equals(id); });
        }

        ///// <summary>
        ///// Gets the repository credential with the specified url.
        ///// The specified Url needn't be the root url.
        ///// </summary>
        ///// <param name="repositoryUrl">The repository URL.</param>
        ///// <returns>The repository credential with the specified url or null if not found.</returns>
        //public RepositoryCredential GetByUrl(string repositoryUrl)
        //{
        //    return Find(delegate(RepositoryCredential repositoryCredential) { return repositoryUrl.StartsWith(repositoryCredential.RootUrl); });
        //}

        /// <summary>
        ///// Gets the repository credential with the specified url, username and password.
        ///// The specified Url needn't be the root url.
        /// </summary>
        /// <param name="repositoryUrl">The repository URL.</param>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        public RepositoryCredential GetByUrlAndUser(string repositoryUrl, string username, string password)
        {
            return this.FirstOrDefault(
                x =>
                repositoryUrl.StartsWith(x.RootUrl) &&
                x.Username.ToLower().Equals(username.ToLower()) &&
                x.Password.Equals(password));
        }

        /// <summary>
        /// Gets all repository credentials for which the specified username and password is valid.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        public List<RepositoryCredential> GetByUser(string username, string password)
        {
            return FindAll(delegate(RepositoryCredential repositoryCredential) { return repositoryCredential.Username.ToLower().Equals(username.ToLower()) && repositoryCredential.Password.Equals(password); });
        }
    }
}
